package com.mfp.java.testYou.core.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FormatUtils {

	private FormatUtils() {
	}

	/**
	 * used to clear a html text of tags. Will leave only the text of length
	 * maxLength.
	 * 
	 * @param text
	 *            original html text
	 * @param maxLength
	 *            maximum length of result text
	 * @return formatted string
	 */
	public static String removeTags(String text, int maxLength) {
        text = text == null ? "" : text;
		// remove tags
		Pattern p = Pattern.compile("<([\\s\\S]*?)>");
		Matcher m = p.matcher(text);
		if (m.find()) {
			text = m.replaceAll("");
		}

		// remove white spaces (other then space)
		Pattern p2 = Pattern.compile("[\\t\\r\\n]");
		Matcher m2 = p2.matcher(text);
		if (m2.find()) {
			text = m2.replaceAll("");
		}

		// trim end
		if (text.length() > maxLength) {
			text = text.substring(0, maxLength) + "...";
		}
		return text;
	}
}
